<?php

namespace july\julyadmin\builder;

/**
 * 表单公共父类
 */
class FormBase
{
    //表单数据
    protected $data = [];
    //是否编辑
    protected $isEdit = 0;
    //表单元素
    public $form;
    //表单元素html
    protected $html = '';
    //表单元素js
    protected $script = '';
    //组件所需js文件
    protected $js = '';
    //组件所需css文件
    protected $css = '';
    //组件css，直接写
    protected $style = '';
    //表单列宽
    protected $colWidth = 10;
    //表单label宽度
    protected $labelWidth = 3;
    //表单input宽度
    protected $inputWidth = 9;
    //是否是查看
    protected $isView = false;
    //是否只读，'readonly'或者''
    protected $readonly = '';
    //items行组件的id模板，最后要替换掉这个模板为实际的每行唯一id
    protected $items_row_id_template = '';
    //items行组件的id模板，最后要替换掉这个模板为实际的每行唯一id，由于有些插件使用的id不能替换为[0]这种，所以单独需要一个模板
    protected $items_row_id_template_fun = '';
    //items行组件的前缀信息，最后要替换掉这个模板，改变items中每个组件的name值
    protected $items_row_prefix = '';
    //items字段名称，如goods
    protected $items_field = '';
    //items组件表头单元格样式
    protected $items_title_style = '';

    /**
     * 设置表单内容
     * @param   string $data  表单内容
     * @return  this
     */
    public function setData($data)
    {
        $this->data = $data;
        return $this;
    }

    /**
     * 设置是否为编辑状态
     * @param   string $isEdit  是否为编辑装填
     * @return  this
     */
    public function setIsEdit($isEdit = 0)
    {
        $this->isEdit = $isEdit;
        return $this;
    }

    /**
     * 设置表单列宽
     * @param   integer $colWidth 列宽
     * @return  this
     */
    public function setColWidth($colWidth = 12)
    {
        $this->colWidth = $colWidth;
        return $this;
    }

    /**
     * 设置form表单label和input宽度
     * @Author   July
     * @DateTime 2024-01-24T21:21:34+0800
     * @param    [type]                   $labelWidth label宽度，1-12
     * @param    [type]                   $inputWidth input宽度，1-12
     * @return   this
     */
    public function size($labelWidth, $inputWidth)
    {
        $this->labelWidth = $labelWidth;
        $this->inputWidth = $inputWidth;
        return $this;

    }

    /**
     * 设置help
     * @param   string $help help内容
     * @return  this
     */
    public function help($help = '')
    {
        $this->form->help = $help;
        return $this;
    }

    /**
     * 设置tips提示信息
     * @Author   July
     * @DateTime 2023-11-25T21:30:08+0800
     * @param    string                   $tips       提示内容
     * @param    integer                  $direction  弹出方向，1向上，2向右，3向下，4向左
     * @param    integer                  $time       自动关闭秒数，比如3，那么在3秒后自动关闭，0为不关闭
     * @param    string                   $background 背景色，如#000000
     * @return   this
     */
    public function tips($tips = '', $direction = 3, $time = 0, $background = '#16b777')
    {
        $this->form->tips = ['tips' => $tips, 'direction' => $direction, 'time' => $time, 'background' => $background];
        return $this;
    }

    /**
     * 设置placecholder
     * @param  string $placeholder
     * @return this
     */
    public function placeholder($placeholder = '')
    {
        $this->form->placeholder = $placeholder;
        return $this;
    }

    /**
     * 设置required
     * @param  string $required
     * @return this
     */
    public function required($required = true)
    {
        $this->form->required = $required;
        return $this;
    }
    /**
     * 设置readonly   只读
     * @param  string $readonly
     * @return this
     */
    public function readonly($readonly = true)
    {
        if ($readonly) {
            $this->readonly = 'readonly';
        } else {
            $this->readonly = '';
        }
        return $this;
    }

    /**
     * 设置是否是查看
     * @param boolean $isView 是否是查看
     */
    public function isView($isView = true)
    {
        $this->isView = $isView;
        return $this;
    }

    /**
     * 设置是否在item中
     * @Author
     * @DateTime 2023-07-01T19:49:20+0800
     * @param    string                  $field items字段名称
     * @return   $this
     */
    public function setItemsField($field = true)
    {
        if ($field) {
            $this->items_row_id_template     = $field ? '_____items_row_id_____' : '';
            $this->items_row_id_template_fun = $field ? '_____items_row_fun_id_____' : '';
            $this->items_row_prefix          = $field ? '_____items_row_prefix_____' : '';
            $this->items_field               = '_' . $field . '_';
        }

        return $this;
    }

    /**
     * 设置表单默认值
     * @param  string $value 默认值
     * @return this
     */
    function default($value = '') {
        if ($this->isEdit) {
            $field = $this->form->field;
            if (!isset($this->data[$field]) || $this->data[$field] === "") {
                $this->form->default = $value;
            }
        } else {
            $this->form->default = $value;
        }
        return $this;
    }

    /**
     * 设置items组件表头单元格样式
     * @Author
     * @DateTime 2023-07-16T01:24:13+0800
     * @param    string                   $style css样式，直接写
     * @return this
     */
    public function itemsTitleStyle($style = '')
    {
        $this->items_title_style = $style;
        return $this;
    }
}
